<?php

// Display Amazon Test Data
add_shortcode( 'book_compare', 'bc_shortcode_display' );

function bc_shortcode_display( $post ) {
	
	// If data is passed process data
	if( isset( $_GET['search'] )) {
		
		// Display the Search Box with the pervious data
		if( isset( $_GET['title'] ))
			bc_search_function( strip_tags($_GET['title']) );
		else
			bc_search_function( strip_tags($_GET['search']) );
		
		// Get the Search Result
		$search = $_REQUEST['search'];
		
		// Test to see if the result is an isbn number
		if (bc_isbn_test(bc_isbn_validator($search)) == 1) {
			
			// if( isset( $_GET['past'])) {	
			// 	// use clearned isbn 
			// 	$past = $_GET['past'];
			// 	bc_amazon_book_results(bc_isbn_validator($search), $past);
			// }
			// else {
				// use cleaned isbn to generate results
				bc_amazon_book_results(bc_isbn_validator($search), '');
			// }
		}
		else {
			// If this is a click from a pervious result get the previous query
			if( isset( $_GET['title'])) {	
				// use keyword to generate results
				$title = $_GET['title'];
				bc_amazon_search_results($search, $title);
			}
			else {
				// use keyword to generate results
				bc_amazon_search_results($search, '');
			}
		}
	}
	// Display the blank form
	else {
		bc_new_search_function();
	}

}

// This function will display the search form with previous value
function bc_new_search_function() {
	$bc_image = plugins_url('images/logo.jpeg', __FILE__);
	?>
	<div id='bc_new_search'>
		<img src="<?php echo $bc_image ?>" width="300px" id="logo" />
		<form action="" method="GET" class="bc_search" style="">
			<input type="text" name="search" id="bc_search" value="Enter Keywords, Authors, Titles or ISBNs" onfocus="if
			(this.value==this.defaultValue) this.value='';" />
			<input type="submit" name="action" id="bc_search_button" value="<?php echo("Search") ?>"  /> 
		</form>	
	</div>
	
	<?php
}

// This function will display the search form with previous value
function bc_search_function( $search_val ) {
	?>
	<div id='bc_search'>
		<form action="" method="GET" class="bc_search" style="">
			<input type="text" name="search" id="bc_search" value="<?php echo( $search_val ) ?>" />
			<input type="submit" name="action" id="bc_search_button" value="<?php echo("Search") ?>"  /> 
		</form>	
	</div>
	
	<?php
}

function bc_isbn_test( $search ) {
	// Take away any - that may be in a ibsn
	$search = trim(str_replace('-', '', $search));

	// If the variable is a 10 or 13 digit number then return true (is isbn)
	// Otherwise return false and search keywords not isbns
	if ( strlen($search) == 10 or strlen($search) == 13 ) {
		if ( is_numeric($search) ) {
			return True;
		}
		else 
			return False;
	}
	else
		return False;
	
}

// This function processes the results from amazon and sends the data to 
// bc_print_search_header to print the images and other details
function bc_amazon_book_results( $bc_isbn, $past ) {
	
	// Gather Data From Amazon
	$bc_amazon_price = bc_amazon_price_gen($bc_isbn);
	
	if( $bc_amazon_price == False) {
		bc_no_result_error($bc_isbn);
	}
	else {
		// Get the Rest of the Display Information
		// $bc_amazon_details = bc_amazon_details_gen($bc_isbn);
		// $bc_amazon_images = bc_amazon_image_gen($bc_isbn);
		
		//echo "Print out the info for an isbn";	
		bc_search_result($bc_amazon_price, $bc_isbn);
		
		// Disply Other Results
		// If there was a previous query submit version 
		// of Amazon Price Gen
		if ( $past != '') {
			$bc_amazon_search_price = bc_amazon_search_price_gen($past);
			bc_search_result_other($bc_amazon_search_price, $bc_isbn);
		}
		
	}
}

// This function processes the results from amazon and sends the data to 
// bc_print_search_header to print the images and other details
function bc_amazon_search_results( $search, $past ) {
	
	// Gather Data From Amazon
	$bc_amazon_search_price = bc_amazon_search_price_gen($search);

	if( $bc_amazon_search_price == false ) {
		bc_no_result_error($search);
	}
	else {
		
		// Display First Result
		bc_search_result($bc_amazon_search_price[0], $search);
		
		// Disply Other Results
		// If there was a previous query submit version 
		// of Amazon Price Gen
		if ( $past != '') {
			$bc_amazon_search_price = bc_amazon_search_price_gen($past);
			bc_search_result_other($bc_amazon_search_price, $search);
		}
		else {
			bc_search_result_other($bc_amazon_search_price, $search);
		}
		
	}
}


function bc_no_result_error($search) {
	echo "<div id='bc_search_error'>Sorry, no results found for: <strong>" . $search . "</strong>. Please try your search again. ";
}




























?>